const app=getApp();
const openid=app.globalData.openid;
Page({
  data: {
    userInfo: '',
  },
  onLoad: function () {
    let user = wx.getStorageSync('user');
    console.log('进入小程序user页面获取缓存', user);
    this.setData({
      userInfo: user
    })

  },

  // 授权登录
  login: function () {
    wx.getUserProfile({
      desc: '必须授权才可以继续使用',
      success: (res) => {
        console.log("授权成功", res.userInfo);
        app.globalData.userInfo=res.userInfo;
        console.log(app.globalData.userInfo);
        let user = res.userInfo;
        // console.log(user)
        wx.request({
          url: `${app.globalData.baseurl}login/openid`,
          method: "POST",
          data: {
            openid: openid,
            username:user.nickName
          },
          success: function (res) {
            console.log(res.data);
          }
        })
        // 把用户信息缓存到本地
        wx.setStorageSync('user', user);
        this.setData({
          userInfo: user,
          
        })

      },
      fail: (res) => {
        console.log("授权失败", res);
      }
    })
  },
  // 退出登录
  loginOut: function () {
    this.setData({
      userInfo: '',
    })
    wx.setStorageSync('user', null)
  },
  toPublic: function () {
    wx.navigateTo({
      url: '../../message/message',
    })
  },
  toMyOrder: function () {
    wx.navigateTo({
      url: '../../myOrder/myOrder',
    })
  },
  toCommentList:function(){
    wx.navigateTo({
      url: '/pages/myComment/myComment',
    })
  },
  toMySaleOut(){
    wx.navigateTo({
      url: '/pages/mySaleOut/mySaleOut',
    })
  }

})